import axios from "axios";
import { Toast } from "antd-mobile";

// axios.defaults.baseURL = "http://157.122.54.189:9060";
axios.defaults.baseURL = "https://api-haoke-web.itheima.net";
axios.defaults.timeout = 15000;
axios.defaults.headers["Content-Type"] = "application/json";

// 请求拦截
axios.interceptors.request.use(
  (config) => {
    // 请求中:
    // Toast.loading('Loading...', 0);
    // config.headers.authorization = user.token //授权(每次请求把token带给后台)
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
axios.interceptors.response.use(
  (response) => {
    // Toast.hide();
    if (response.data.status === 400) {
      Toast.fail(response.data.description);
    }
    return response;
  },
  (error) => {
    Toast.fail(error.message);
    // return Promise.resolve(error.message);
    return Promise.reject(error.message);
  }
);

export default {
  get(url, params) {
    // 返回promise对象
    return axios({
      method: "get",
      url,
      params,
    });
  },
  post(url, params) {
    return axios({
      method: "post",
      url,
      data: params,
    });
  },
};
